package com.caipiao.lottery.lty.handler.impl;

import com.caipiao.lottery.config.Config;
import com.caipiao.lottery.core.ErrorDef;
import com.caipiao.lottery.data.UserInfo;
import com.caipiao.lottery.dbwarpper.P_API_USER;
import com.caipiao.lottery.log.TraceLogger;
import com.caipiao.lottery.lty.handler.LtyHandler;
import com.caipiao.lottery.lty.message.LtyRequest;
import com.caipiao.lottery.lty.message.LtyResponse;
import com.caipiao.lottery.lty.message.bean.LtyMsgBodySHR1000;
import jodd.util.StringUtil;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 用户发信息
 */
public class LtyHandlerSHR1000 implements LtyHandler {
    public static void main(String[] args) throws Exception{
        System.out.println(new String("的客服解释道烦死了都烦死了都发了".getBytes("GBK"), "utf-8"));
    }

    @Override
    public String DoProcess(LtyRequest ltyReq, LtyResponse ltyResp) throws Exception {

        LtyMsgBodySHR1000.Req reqBody = (LtyMsgBodySHR1000.Req) ltyReq.bodyMsg;
        LtyMsgBodySHR1000.Resp respBody = new LtyMsgBodySHR1000.Resp();

        //查询信息
        UserInfo userInfo = new UserInfo();
        boolean db_r = P_API_USER.prGetUserInfo(ltyReq.msgId, reqBody.machineCode,
                reqBody.regCode, userInfo);

        if (!db_r) {
            throw new Exception("数据库调用 P_API_USER.prGetUserInfo 失败");
        }
        TraceLogger.Lty.debug(ltyReq.msgId + " 查询{regCode:"+reqBody.regCode+"}成功");

        if(1 > userInfo.count){
            respBody.result = "-1";
            respBody.resultDesc = "注册码输入错误，购买请联系QQ:";
            return ErrorDef.SYS_SUCESS;
        }

        long userExpTime = new SimpleDateFormat("yyyyMMddHHmmss").parse(userInfo.expTime).getTime();
        long sysTime = new Date().getTime();
        if(userExpTime <= sysTime){
            respBody.result = "-1";
            respBody.resultDesc = "注册码已过期，购买请联系QQ:";
            return ErrorDef.SYS_SUCESS;
        }

        //判断在线用户数
        int onlineUserCount = Integer.parseInt(Config.Init.GetPropValue("onlineUserCount"));
        Map < String, Integer > result = new HashMap< String, Integer >();
        db_r = P_API_USER.queryOnlineUserCount(ltyReq.msgId, userInfo.id, result) ;
        if (!db_r) {
            throw new Exception("数据库调用 P_API_USER.queryOnlineUserList 失败");
        }
        //判断是否超出
        if(onlineUserCount - 1 < result.get("count")){
            respBody.result = "-1";
            respBody.resultDesc = "系统用户过多，请稍后再试，购买请联系QQ:";
            return ErrorDef.SYS_SUCESS;
        }

        List<String> mess = new ArrayList<String>();
        String[] messArr = reqBody.mess.split("\n\r");
        for(String str:messArr){
            if(StringUtil.isBlank(str)){
                continue;
            }
            mess.add(str);
        }
        CookieManager.Instance().sendMess(ltyReq.msgId, userInfo, reqBody.url, mess);

        //修改用户在线状态
        db_r = P_API_USER.updateUserOnlineStatus(ltyReq.msgId, userInfo.id, "1");

        if (!db_r) {
            throw new Exception("数据库调用 P_API_USER.updateUserOnlineStatus 失败");
        }
        TraceLogger.Lty.debug(ltyReq.msgId + " 修改用户["+userInfo.id+"]在线状态成功");

        respBody.result = "0";
        respBody.resultDesc = "操作成功！";

        ltyResp.bodyMsg = respBody;

        return ErrorDef.SYS_SUCESS;
    }
}
